/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.baidu.aip.fl.utils;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.cookie.CookieJarImpl;
import com.zhy.http.okhttp.cookie.store.PersistentCookieStore;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by liuguohai on 2017/3/24.
 */

public class HttpUtilliu {

    public static String BASE_HOST = "";
    private static Context mContext;



    public static void setBaseHost(String baseHost) {
        BASE_HOST = baseHost;
    }

    public static void setCookine(Context context) {
        CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(context));
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }


    public static void getAnyTheng(String strurl, Map<String, String> params, Callback callback) {
        String url = getAbsoluteUrl(strurl);
        OkHttpUtils
                .get()
                .url(url)
                .params(params)
                .build().execute(callback);
    }
    public static void getcode(String strurl, Map<String, String> params, Callback callback) {
        OkHttpUtils
                .get()
                .url(strurl)
                .params(params)
                .build().execute(callback);
    }
    public static void postfacetoken(String strurl, Map<String, String> params, Callback callback) {
        String url =strurl;
        OkHttpUtils
                .post()
                .url(url)

                .params(params)
                .build().execute(callback);
    }

    public static void facesecarh(String strurl, Map<String, String> params, Callback callback) {
        String url =strurl;
        OkHttpUtils
                .post()
                .url(url)
                .addHeader("Content-Type","application/json")
                .params(params)
                .build().execute(callback);
    }

    public static void regist(String strurl, Map<String, String> params, Callback callback) {
        String url = getAbsoluteUrl(strurl);
        OkHttpUtils
                .post()
                .url(url)
                .params(params)

                .build().execute(callback);
    }


    public static void postString(String strurl, String gson, Callback callback) {
        String url = getAbsoluteUrl(strurl);
        OkHttpUtils
                .postString()
                .url(url)
                .content(gson)
                .build().execute(callback);
    }

    //上传文件和参数
    public static void uploadFileAndParams(String strurl, String filename, String fileurl, File file, Map<String, String> params, Map<String, String> headers, Callback callback) {
        String url = getAbsoluteUrl(strurl);
        OkHttpUtils
                .post()
                .addFile(filename, fileurl, file)
                .params(params)
                .headers(headers)
                .url(url)
                .build().execute(callback);
    }

    //上传文件和参数
    public static void uploadFilepic(String strurl, String filename, String fileurl, File file, Callback callback) {
        String url = getAbsoluteUrl(strurl);
        OkHttpUtils
                .post()
                .addFile(filename, fileurl, file)
                .url(url)
                .build().execute(callback);
    }



    //下载文件
    public void downloadFile(String strurl, FileCallBack callback) {
        OkHttpUtils
                .get()
                .url(getAbsoluteUrl(strurl))
                .build()
                .execute(callback);
    }


    /**
     * 获取请求链接
     *
     * @param relativeUrl RESTful 接口key
     */
    public static String getAbsoluteUrl(String relativeUrl) {
        if (relativeUrl.startsWith("http")) {
            return relativeUrl;
        } else {
            return BASE_HOST + relativeUrl;
        }
    }
}
